
import threading
import serial.tools.list_ports
import serial
import time 
import sys
from tkinter import messagebox

class Comm_thread (threading.Thread):
    def __init__(self, window):
        threading.Thread.__init__(self)
        self.window = window

    def convert(self, act):
        s = ''
        if act[0] == "正":
            s = s+'C'
        elif act[0] == "反":
            s = s+'I'
        else:
            s = s+'T'
        s=s+' '
        
        if act[1] == "快":
            s = s+'H'
        elif act[1] == "中":
            s = s+'M'
        elif act[1] == "慢":
            s = s+'L'
        
        s=s+' '
        time_s = act[2:]
        s = s+time_s
        
        b = str.encode(s)
        return b

    def start_connect(self):
        chosen_port = self.window.get_comx_cb()
        if chosen_port[4] == ' ':
            chosen_port = chosen_port[0:4]
        else:
            chosen_port = chosen_port[0:5]
        # print(chosen_port[0:3])
        # print("opening " + chosen_port)
        # print(self.get_baud_cb())
        
        try:
            self.window.ser = serial.Serial(chosen_port, self.window.get_baud_cb(),
                        bytesize=int(self.window.databit_cb.get()), parity=self.window.paritybit_cb.get()[0], stopbits=1, timeout=1)
        except:
            pass
        # for i in range(30):
        #     try:
        #         self.ser.open(chosen_port, self.get_baud_cb(),
        #                         bytesize=8, parity=self.paritybit_cb.get()[0], databits=1, timeout=1)
        #     except:
        #         pass
        try:
            if self.window.ser.isOpen() == True:
                self.window.btn_connect['text'] = "关闭串口"
                self.window.connect_sign = False

            else:
                print("没开开")
        except:
            pass
        # bytesize：FIVEBITS、SIXBITS、SEVENBITS、EIGHTBITS
        # paritybit: paritybit_NONE, paritybit_EVEN, paritybit_ODD, paritybit_MARK, paritybit_SPACE
        # databits: databitS_ONE, databitS_ONE_POINT_FIVE, databitS_TWO    
    def run(self):
        while True:
            if self.window.connect_sign == True:
                self.start_connect()
            if self.window.get_auto_state() == True:
                act = self.window.listb.get(0, 1)
                if len(act) > 0:
                    self.window.ser.write(bytes([0x80]))
                    b = self.convert(act[0])
                    self.window.ser.write(b)
                    print(b)
                    if len(act) == 2:
                        
                        b = bytes([0x3E])+self.convert(act[1])
                        self.window.ser.write(b)
                        print(b)
                    self.window.ser.write(bytes([0xff]))
                    
                    time_s = act[0][2:]
                    print(time_s)
                    minute = int(time_s[0])
                    second = int(time_s[2:4])
                    milli = int(time_s[5])
                    time_d = (minute*60*10+second*10+milli)/10.0
                    print(time_d)
                    time.sleep(time_d)
                    self.window.listb.delete(0)
                if len(self.window.listb.get(0, 1)) <= 0:
                    s = "T"+' '*16
                    b = bytes([0x80])+str.encode(s)+bytes([0xff])
                    self.window.ser.write(b)
                    self.window.btn_auto["text"] = "开始顺序动作"
                    self.window.auto_state = False
                    messagebox.showinfo( "提示", "执行完毕，无剩余动作序列")
                    

            time.sleep(0.1)
# 写一个按钮，用来控制发送使能
# 循环判断发送使能标志，如果使能了，就取头一个，作为控制指令发送，如果有第二个，把第二个也拉出来发送

# 发送后，立即进入延时等待，延时阶数后，把数字加一

